package com.etlmaster.tree;

import java.util.ArrayList;
import java.util.List;

public class Test {
	private static List<String> allPath=new ArrayList<String>();
	public static void main(String[] args) {
		TNode node1 = new TNode("2");
		TNode node2 = new TNode("4");
		TNode node3 = new TNode("1");
		TNode node4 = new TNode("3");
		TNode node5 = new TNode("9");
		TNode node6 = new TNode("0");

		List<TNode> child1 = new ArrayList<TNode>();
		child1.add(node2);
		child1.add(node3);
		List<TNode> child2 = new ArrayList<TNode>();
		child2.add(node4);
		child2.add(node5);
		List<TNode> child3 = new ArrayList<TNode>();
		child3.add(node6);

		node1.setChildren(child1);

		node2.setChildren(child2);
		node2.setParent(node1);

		node3.setChildren(child3);
		node3.setParent(node1);

		node4.setParent(node2);
		node5.setParent(node2);

		node6.setParent(node3);
		List<TNode> allNode = new ArrayList<TNode>();
		allNode.add(node1);
		allNode.add(node2);
		allNode.add(node3);
		allNode.add(node4);
		allNode.add(node5);
		allNode.add(node6);
		TNode rootNode=node1;
		getPath(rootNode,"");
		System.out.println("所有路径如下：");

		for(String path:allPath){
			System.out.println(path);
		}
		System.out.println("其中最大路径为：");

		System.out.print(getLargestPath());
	}

	public static void getPath(TNode node,String path){
		if(path.length()>0)
			path+=",";
		path+=node.getId();
		if(node.getChildren()!=null){
			for(TNode childNode:node.getChildren()){
				getPath(childNode,path);
			}
		}else{
			allPath.add(path);
		}
	}
	public static String getLargestPath(){
		int largestInt=0;
		String largestPath="";
		for(String path:allPath){
			String []paths=path.split(",");
			int totalInt=0;
			for(String value:paths){
				totalInt+=Integer.valueOf(value);
			}
			if(largestInt<totalInt){
				largestInt=totalInt;
				largestPath=path;
			}
		}
		return largestPath;
		
	}

}
